package com.starleague.hexagonal.android;

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.starleague.android.event.HasEventBus;
import com.starleague.battletech.hexagonal.IBattletechHex;
import com.starleague.battletech.hexagonal.IBattletechHexTerrain;
import com.starleague.hexagonal.android.event.HexSelectedEvent;

/**
 * HexDetailView: A View that contains info about a single Hex
 */
public class HexDetailView
        extends View
// implements GestureDetector.OnGestureListener// ,
// GestureDetector.OnDoubleTapListener
{
    // private final Rect drawingRect = new Rect();
    private final TextPaint textPaint = new TextPaint();

    private final EventBus eventBus;
    // private BattletechActivity battletechActivity;
    private IBattletechHex selectedHex = null;

    public HexDetailView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public HexDetailView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

        Log.w("INFO", "HexDetailView.ctor(): " + context);

        if (context instanceof HasEventBus)
        {
            this.eventBus = ((HasEventBus) context).getEventBus();
        }
        else
        {
            throw new RuntimeException("HexGridView.initView(): context is not a [HasEventBus]");
        }

        // if (context instanceof BattletechActivity)
        // {
        // this.battletechActivity = (BattletechActivity) context;
        // }
        // else
        // {
        // throw new RuntimeException("HexGridView.initView(): context is not a [BattletechActivity]");
        // }

        setFocusable(true);
    }

    @Override
    protected void onVisibilityChanged(View changedView,
                                       int visibility)
    {
        Log.w("INFO", "HexDetailView.onVisibilityChanged: [" + visibility + "]: " + changedView);
        super.onVisibilityChanged(changedView, visibility);
    }

    @Override
    protected void onAttachedToWindow()
    {
        Log.w("INFO", "HexDetailView.onAttachedToWindow()");
        super.onAttachedToWindow();

        this.eventBus.register(this);
    }

    @Override
    protected void onDetachedFromWindow()
    {
        Log.w("INFO", "HexDetailView.onDetachedFromWindow()");

        this.eventBus.unregister(this);

        super.onDetachedFromWindow();
    }

    @Subscribe
    public void handleHexSelectedEvent(HexSelectedEvent event)
    {
        Log.w("INFO", "HexDetailView.handleHexSelectedEvent: " + event);
        this.selectedHex = (IBattletechHex) event.getHex();
        this.invalidate();
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        // TextView
        super.onDraw(canvas);
        // Log.w("INFO", "onDraw Scroll XY: " + getScrollX() + ", " +
        // getScrollY());
        // getDrawingRect(this.drawingRect);

        Log.w("INFO", "onDraw HexDetails");
        // Log.w("INFO", "onDraw Rect: " + this.drawingRect);

        if (this.selectedHex != null)
        {
            canvas.drawText("COORD: " + this.selectedHex.getHexCoordinate(), 10, 20, this.textPaint);
            // TODO Do this in a subclass!
            canvas.drawText("ELEVATION: " + this.selectedHex.getElevation(), 10, 50, this.textPaint);
            int canvasY = 50;
            for (IBattletechHexTerrain hexTerrain : this.selectedHex.getHexTerrainList())
            {
                canvas.drawText("TERRAIN: "
                                        + hexTerrain.getHexTerrainType()
                                        + " ("
                                        + hexTerrain.getLevel()
                                        + ")",
                                10,
                                canvasY += 30,
                                this.textPaint);
            }
        }
        else
        {
            canvas.drawText("COORD: NONE", 10, 20, this.textPaint);
        }
    }
}
